package com.hutra.utils 
{
	/**
	 * 11/5/2011 12:49 PM
	 * @version 1.0.0.1	
	 * @author hungconcon
	 */
	public class DateUtilities 
	{
		/**
		 * qryHowOld                                                                  
		 * Description: How old someone is in the format:                             
		 * XXX Years XX Months X Weeks X Days                                         
		 * Birth Date could be specified like Date.UTC(2002,8,16,17,42,0)             
		 *                                                                            
		 * Arguments:                                                                 
		 *    varAsOfDate: as of date                                                 
		 *    varBirthDate: birth date                                                
		 */
		public function DateUtilities() 
		{
			//year,month,day,hours,minutes,seconds,ms
			
		}
		public var olyday:String;
		/**
		 * 
		 * @param	varAsOfDate ~ year,month,day,hours,minutes,seconds,ms
		 * @param	varBirthDate ~ year,month,day,hours,minutes,seconds,ms
		 * @return
		 */		                                                                            
		public function qryHowOld(varAsOfDate:Number, varBirthDate:Number):String 
	    {
		   var dtAsOfDate:Date;
		   var dtBirth:Date;
		   var dtAnniversary:Date;
		   var intSpan:Number;
		   var intYears:Number;
		   var intMonths:Number;
		   var intWeeks:Number;
		   var intDays:Number;
		   var intHours:Number;
		   var intMinutes:Number;
		   var intSeconds:Number;
		   var strHowOld:String;

		   // get born date
		   dtBirth = new Date(varBirthDate);
		   
		   // get as of date
		   dtAsOfDate = new Date(varAsOfDate);

		   // if as of date is on or after born date
		   if ( dtAsOfDate >= dtBirth )
			  {

			  // get time span between as of time and birth time
			  intSpan = ( dtAsOfDate.getUTCHours() * 3600000 +
						  dtAsOfDate.getUTCMinutes() * 60000 +
						  dtAsOfDate.getUTCSeconds() * 1000    ) -
						( dtBirth.getUTCHours() * 3600000 +
						  dtBirth.getUTCMinutes() * 60000 +
						  dtBirth.getUTCSeconds() * 1000       )

			  // start at as of date and look backwards for anniversary 

			  // if as of day (date) is after birth day (date) or
			  //    as of day (date) is birth day (date) and
			  //    as of time is on or after birth time
			  if ( dtAsOfDate.getUTCDate() > dtBirth.getUTCDate() ||
				   ( dtAsOfDate.getUTCDate() == dtBirth.getUTCDate() && intSpan >= 0 ) )
				 {

				 // most recent day (date) anniversary is in as of month
				 dtAnniversary =  new Date( Date.UTC(   dtAsOfDate.getUTCFullYear(),
														dtAsOfDate.getUTCMonth(),
														dtBirth.getUTCDate(),
														dtBirth.getUTCHours(),
														dtBirth.getUTCMinutes(),
														dtBirth.getUTCSeconds() ) );

				 }

			  // if as of day (date) is before birth day (date) or
			  //    as of day (date) is birth day (date) and
			  //    as of time is before birth time
			  else
				 {

				 // most recent day (date) anniversary is in month before as of month
				 dtAnniversary = 
					new Date( Date.UTC( dtAsOfDate.getUTCFullYear(),
										dtAsOfDate.getUTCMonth() - 1,
										dtBirth.getUTCDate(),
										dtBirth.getUTCHours(),
										dtBirth.getUTCMinutes(),
										dtBirth.getUTCSeconds() ) );

				 // get previous month
				 intMonths = dtAsOfDate.getUTCMonth() - 1;
				 if ( intMonths == -1 )
					intMonths = 11;

				 // while month is not what it is supposed to be (it will be higher)
				 while ( dtAnniversary.getUTCMonth() != intMonths )

					// move back one day
					dtAnniversary.setUTCDate( dtAnniversary.getUTCDate() - 1 );

				 }

			  // if anniversary month is on or after birth month
			  if ( dtAnniversary.getUTCMonth() >= dtBirth.getUTCMonth() )
				 {

				 // months elapsed is anniversary month - birth month
				 intMonths = dtAnniversary.getUTCMonth() - dtBirth.getUTCMonth();

				 // years elapsed is anniversary year - birth year
				 intYears = dtAnniversary.getUTCFullYear() - dtBirth.getUTCFullYear();

				 }

			  // if birth month is after anniversary month
			  else
				 {

				 // months elapsed is months left in birth year + anniversary month
				 intMonths = (11 - dtBirth.getUTCMonth()) + dtAnniversary.getUTCMonth() + 1;

				 // years elapsed is year before anniversary year - birth year
				 intYears = (dtAnniversary.getUTCFullYear() - 1) - dtBirth.getUTCFullYear();

				 }

			  // to calculate weeks, days, hours, minutes and seconds
			  // we can take the difference from anniversary date and as of date

			  // get time span between two dates in milliseconds
			  intSpan = dtAsOfDate.getTime() - dtAnniversary.getTime();
			
			  olyday = new Dater().toTimeStringLess(intSpan, true);
			  
			  // get number of weeks
			  intWeeks = Math.floor(intSpan / 604800000);

			  // subtract weeks from time span
			  intSpan = intSpan - (intWeeks * 604800000);
			  
			  // get number of days
			  intDays = Math.floor(intSpan / 86400000);
				
			  // subtract days from time span
			  intSpan = intSpan - (intDays * 86400000);

			  // get number of hours
			  intHours = Math.floor(intSpan / 3600000);
			
			  // subtract hours from time span
			  intSpan = intSpan - (intHours * 3600000);

			  // get number of minutes
			  intMinutes = Math.floor(intSpan / 60000);

			  // subtract minutes from time span
			  intSpan = intSpan - (intMinutes * 60000);

			  // get number of seconds
			  intSeconds = Math.floor(intSpan / 1000);

			  // create output string     
			  if ( intYears > 0 )
				 if ( intYears > 1 )
					strHowOld = intYears.toString() + ' years';
				 else
					strHowOld = intYears.toString() + ' year';
			  else
				 strHowOld = '';

			  if ( intMonths > 0 )
				 if ( intMonths > 1 )
					strHowOld = strHowOld + ' ' + intMonths.toString() + ' months';
				 else
					strHowOld = strHowOld + ' ' + intMonths.toString() + ' month';
				   
			  if ( intWeeks > 0 )
				 if ( intWeeks > 1 )
					strHowOld = strHowOld + ' ' + intWeeks.toString() + ' weeks';
				 else
					strHowOld = strHowOld + ' ' + intWeeks.toString() + ' week';

			  if ( intDays > 0 )
				 if ( intDays > 1 )
					strHowOld = strHowOld + ' ' + intDays.toString() + ' days';
				 else
					strHowOld = strHowOld + ' ' + intDays.toString() + ' day';

			  if ( intHours > 0 )
				 if ( intHours > 1 )
					strHowOld = strHowOld + ' ' + intHours.toString() + ' hours';
				 else
					strHowOld = strHowOld + ' ' + intHours.toString() + ' hour';
		 
			  if ( intMinutes > 0 )
				 if ( intMinutes > 1 )
					strHowOld = strHowOld + ' ' + intMinutes.toString() + ' minutes';
				 else
					strHowOld = strHowOld + ' ' + intMinutes.toString() + ' minute';

			  if ( intSeconds > 0 )
				 if ( intSeconds > 1 )
					strHowOld = strHowOld + ' ' + intSeconds.toString() + ' seconds';
				 else
					strHowOld = strHowOld + ' ' + intSeconds.toString() + ' second';

			  }
		   else
			  strHowOld = 'not born yet';

		   // return string representation
		   return strHowOld
	   }
	}
}